<?php
/**
 * Created by PhpStorm.
 * User: Dell
 * Date: 2018-12-25
 * Time: 9:42
 */

namespace app\common\model;

use think\Model;

class GoodsModel extends Model
{
    // 确定链接表名
    protected $name = 'goods';

    /**
     * 查询商品
     * @param $where
     * @param $offset
     * @param $limit
     */
    public function getGoodsByWhere($where, $offset, $limit, $field="*")
    {
        return $this->where($where)->field($field)->limit($offset, $limit)->order('good_id desc')->select();
    }

    /**
     * 根据搜索条件获取所有的商品数量
     * @param $where
     */
    public function getAllGoods($where)
    {
        return $this->where($where)->count();
    }

    /**
     * 添加商品
     * @param $param
     */
    public function addGoods($param,$url='goods/index')
    {
        try{

            //查重
            $res = $this->checkGoods($param);

            if ($res != NULL){
                return json(msg(-1, '', '该商品已经存在'));

            }
            $result = $this->validate('GoodsValidate')->save($param);
            if(false === $result){
                // 验证失败 输出错误信息
                return msg(-1, '', $this->getError());
            }else{

                return msg(1, url($url), '添加商品成功');
            }
        }catch (\Exception $e){
            return msg(-2, '', $e->getMessage());
        }
    }

    /**
     * 编辑商品信息
     * @param $param
     */
    public function editGoods($param,$url='goods/index')
    {
        try{

            //查重
            $res = $this->checkGoods($param);

            if ($res != NULL){
                return msg(-1, '', '该商品已经存在');

            }

            $result = $this->validate('GoodsValidate')->save($param, ['good_id' => $param['good_id']]);
            if(false === $result){
                // 验证失败 输出错误信息
                return msg(-1, '', $this->getError());
            }else{

                return msg(1, url($url), '编辑商品成功');
            }
        }catch(\Exception $e){
            return msg(-2, '', $e->getMessage());
        }
    }

    /**
     * 根据商品的id 获取商品的信息
     * @param $id
     */
    public function getOneGoods($id,$field="*")
    {
        return $this->where('good_id', $id)->field($field)->find();
    }

    /**
     * 删除商品
     * @param $id
     */
    public function delGoods($id)
    {
        try{

            $result = $this->save(['good_delete'=>1], ['good_id' => $id]);

            if(false === $result){
                // 验证失败 输出错误信息
                return msg(-1, '', $this->getError());
            }else{

                return msg(1, url('goods/index'), '删除商品成功');
            }

        }catch(\Exception $e){
            return msg(-1, '', $e->getMessage());
        }
    }
    /**
     * 查重
     *
     */
    public function checkGoods($data){

        $map['good_id'] = array('neq',(isset($data['good_id'])?$data['good_id']:0));
        $map['good_name'] = array('eq',$data['good_name']);
        $map['good_type'] = array('eq',$data['good_type']);
        $map['good_classify'] = array('eq',$data['good_classify']);
        $map['good_delete'] = array('eq',0);
        return $this->where($map)->find();

    }
    /**
     * 查询点击数最高商标前三
     * @param $where
     * @param $offset
     * @param $limit
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getGoodsByHits($where, $offset, $limit,$field="*")
    {
        return $this->where($where)->limit($offset, $limit)->field($field)->order('good_hits desc')->select();
    }

}